/*
 * @Author: szx
 * @Date: 2022-06-11 13:49:26
 * @LastEditTime: 2022-06-12 19:38:19
 * @Description:
 * @FilePath: \leetcode\面试题\16\24\24.js
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[][]}
 */
var pairSums = function (nums, target) {
    const map = new Map(),
        res = [];
    for (let num of nums) {
        const tar = target - num;
        if (map.has(tar)) {
            res.push([tar, num]);
            if (map.get(tar) === 1) map.delete(tar);
            else map.set(tar, map.get(tar) - 1);
        } else {
            map.set(num, (map.get(num) || 0) + 1);
        }
    }
    return res;
};

var pairSums = function (nums, target) {
    nums.sort((a, b) => a - b);
    const res = [];
    let l = 0,
        r = nums.length - 1;
    while (l < r) {
        if (nums[l] + nums[r] === target) {
            res.push([nums[l], nums[r]]);
            l++;
            r--;
        } else if (nums[l] + nums[r] < target) l++;
        else r--;
    }
    return res;
};
